#include"Acceptor.h"

Acceptor::Acceptor(EventLoop *loop,const uint16_t port)
:loop_(loop)
{
    servSock_ = new Sock();
    // 服务器的只需要这个port就行
    InetAddress servaddr(port);
    // 创建非阻塞的sock
    servSock_->cerateNonBlockingSocket();
    servSock_->setKeepAlive(true);
    servSock_->setReuseAddr(true);
    servSock_->setReusePort(true);
    servSock_->settcpnodelay(true);
    servSock_->bind(servaddr);
    servSock_->listen();

    acceptChannel_ = new Channel(loop_, servSock_->getSock());
    acceptChannel_->setReadCallBack(std::bind(&Acceptor::newconnection,this));
    acceptChannel_->enableReading();
}

Acceptor::~Acceptor()
{
    delete servSock_;
    delete acceptChannel_;
}

void Acceptor::newconnection()
{
    InetAddress clientaddr;
    Sock *clentSock = new Sock(servSock_->acceptNonBlocking(clientaddr));
    printf("accept client(fd=%d,ip=%s,port=%d) ok.\n", clentSock->getSock(), clientaddr.ip(), clientaddr.port());
    Connection *conn=new Connection(loop_,clentSock);
}
